home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / public / GNU / emacs.inst / emacs19.idb / usr / gnu / lib / emacs / site-lisp / auto-save.el.z / auto-save.el
Encoding:
Text File  |  1994-08-02  |  17.6 KB  |  466 lines

  1. ;;;; auto-save.el - safer auto saving with support for ange-ftp and /tmp
  2.  
  3. (defconst auto-save-version (substring "$Revision: 1.13 $" 11 -2)
  4.   "$Id: auto-save.el,v 1.13 1992/04/22 17:04:55 sk Exp $")
  5.  
  6. ;;;; Copyright (C) 1992 by Sebastian Kremer <sk@thp.uni-koeln.de>
  7.  
  8. ;;;; This program is free software; you can redistribute it and/or modify
  9. ;;;; it under the terms of the GNU General Public License as published by
  10. ;;;; the Free Software Foundation; either version 1, or (at your option)
  11. ;;;; any later version.
  12. ;;
  13. ;;;; This program is distributed in the hope that it will be useful,
  14. ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16. ;;;; GNU General Public License for more details.
  17. ;;
  18. ;;;; You should have received a copy of the GNU General Public License
  19. ;;;; along with this program; if not, write to the Free Software
  20. ;;;; Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21.  
  22. ;;;; LISPDIR ENTRY for the Elisp Archive ===============================
  23. ;;;;    LCD Archive Entry:
  24. ;;;;    auto-save|Sebastian Kremer|sk@thp.uni-koeln.de
  25. ;;;;    |safer auto saving with support for ange-ftp and /tmp
  26. ;;;;    |$Date: 1992/04/22 17:04:55 $|$Revision: 1.13 $|
  27.  
  28. ;;;; OVERVIEW ==========================================================
  29.  
  30. ;;;; Combines autosaving for ange-ftp (to a local or remote directory)
  31. ;;;; with the ability to do autosaves to a fixed directory on a local
  32. ;;;; disk, in case NFS is slow.  The auto-save file used for
  33. ;;;;     /usr/foo/bar/baz.txt
  34. ;;;; will be
  35. ;;;;     AUTOSAVE/#\!usr\!foo\!bar\!baz.txt#
  36. ;;;; assuming AUTOSAVE is the non-nil value of the variable
  37. ;;;; `auto-save-directory'.
  38.  
  39. ;;;; Takes care that autosave files for non-file-buffers (e.g. *mail*)
  40. ;;;; from two simultaneous Emacses don't collide.
  41.  
  42. ;;;; Autosaves even if the current directory is not writable.
  43.  
  44. ;;;; Can limit autosave names to 14 characters using a hash function,
  45. ;;;; see `auto-save-hash-p'.
  46.  
  47. ;;;; See `auto-save-directory' and `make-auto-save-file-name' and
  48. ;;;; references therein for complete documentation.
  49.  
  50. ;;;; Meta-x recover-all-files will effectively do recover-file on all
  51. ;;;; files whose autosave file is newer (one of the benefits of having
  52. ;;;; all autosave files in the same place).
  53.  
  54. ;;;; INSTALLATION ======================================================
  55.  
  56. ;;;; Put this file into your load-path and the following in your ~/.emacs:
  57.  
  58. ;;;; If you want to autosave in the fixed directory /tmp/USER-autosave/
  59. ;;;; (setq auto-save-directory
  60. ;;;;       (concat "/tmp/" (user-login-name) "-autosave/"))
  61.  
  62. ;;;; If you don't want to save in /tmp (e.g., because it is swap
  63. ;;;; mounted) but rather in ~/autosave/
  64. ;;;;   (setq auto-save-directory (expand-file-name "~/autosave/"))
  65.  
  66. ;;;; If you want to save each file in its own directory (the default)
  67. ;;;;   (setq auto-save-directory nil)
  68. ;;;; You still can take advantage of autosaving ange-ftp remote files
  69. ;;;; in a fixed local directory, `auto-save-directory-fallback' will
  70. ;;;; be used.
  71.  
  72. ;;;; If you want to use 14 character hashed autosave filenames
  73. ;;;;   (setq auto-save-hash-p t)
  74.  
  75. ;;;; Finally, put this line after the others in your ~/.emacs:
  76. ;;;;   (require 'auto-save)
  77.  
  78.  
  79. ;;;; ACKNOWLEDGEMENT ===================================================
  80.  
  81. ;;;; This code is loosely derived from autosave-in-tmp.el by Jamie
  82. ;;;; Zawinski <jwz@lucid.com> (the version I had was last modified 22
  83. ;;;; dec 90 jwz) and code submitted to ange-ftp-lovers on Sun, 5 Apr
  84. ;;;; 92 23:20:47 EDT by drw@BOURBAKI.MIT.EDU (Dale R. Worley).
  85. ;;;; auto-save.el tries to cover the functionality of those two
  86. ;;;; packages.
  87.  
  88. ;;;; Valuable comments and help from Dale Worley, Andy Norman, Jamie
  89. ;;;; Zawinski and Sandy Rutherford are gratefully acknowledged.
  90.  
  91. ;;;; CUSTOMIZATION =====================================================
  92.  
  93. (defvar auto-save-directory nil
  94.  
  95.   ;;; Don't make this user-variable-p, it should be set in .emacs and
  96.   ;;; left at that.  In particular, it should remain constant across
  97.   ;;; several Emacs session to make recover-all-files work.
  98.  
  99.   "If non-nil, fixed directory for autosaving: all autosave files go
  100. there.  If this directory does not yet exist at load time, it is
  101. created and its mode is set to 0700 so that nobody else can read your
  102. autosave files.
  103.  
  104. If nil, each autosave files goes into the same directory as its
  105. corresponding visited file.
  106.  
  107. A non-nil `auto-save-directory' could be on a local disk such as in
  108. /tmp, then auto-saves will always be fast, even if NFS or the
  109. automounter is slow.  In the usual case of /tmp being locally mounted,
  110. note that if you run emacs on two different machines, they will not
  111. see each other's auto-save files.
  112.  
  113. The value \(expand-file-name \"~/autosave/\"\) might be better if /tmp
  114. is mounted from swap (possible in SunOS, type `df /tmp' to find out)
  115. and thus vanishes after a reboot, or if your system is particularly
  116. thorough when cleaning up /tmp, clearing even non-empty subdirectories.
  117.  
  118. It should never be an ange-ftp remote filename because that would
  119. defeat `ange-ftp-auto-save-remotely'.
  120.  
  121. Unless you set `auto-save-hash-p', you shouldn't set this to a
  122. directory in a filesystem that does not support long filenames, since
  123. a file named
  124.  
  125.     /home/sk/lib/emacs/lisp/auto-save.el
  126.  
  127. will have a longish filename like
  128.  
  129.     AUTO-SAVE-DIRECTORY/#\\!home\\!sk\\!lib\\!emacs\\!lisp\\!auto-save.el#
  130.  
  131. as auto save file.
  132.  
  133. See also variables `auto-save-directory-fallback', `auto-save-hash-p',
  134. `ange-ftp-auto-save' and `ange-ftp-auto-save-remotely'.")
  135.  
  136. (defvar auto-save-hash-p nil
  137.   "If non-nil, hashed autosave names of length 14 are used.
  138. This is to avoid autosave filenames longer than 14 characters.
  139. The directory used is `auto-save-hash-directory' regardless of
  140. `auto-save-directory'.
  141. Hashing defeats `recover-all-files', you have to recover files
  142. individually by doing `recover-file'.")
  143.  
  144. ;;; This defvar is in ange-ftp.el now, but for older versions it
  145. ;;; doesn't hurt to give it here as well so that loading auto-save.el
  146. ;;; does not abort.
  147. (defvar ange-ftp-auto-save 0
  148.   "If 1, allows ange-ftp files to be auto-saved.
  149. If 0, suppresses auto-saving of ange-ftp files.
  150. Don't use any other value.")
  151.  
  152. (defvar ange-ftp-auto-save-remotely nil
  153.   "*If non-nil, causes the auto-save file for an ange-ftp file to be written in
  154. the remote directory containing the file, rather than in a local directory.
  155.  
  156. For remote files, this being true overrides a non-nil
  157. `auto-save-directory'.  Local files are unaffected.
  158.  
  159. If you want to use this feature, you probably only want to set this
  160. true in a few buffers, rather than globally.  You might want to give
  161. each buffer its own value using `make-variable-buffer-local'.
  162.  
  163. See also variable `ange-ftp-auto-save'.")
  164.  
  165. ;;;; end of customization
  166.  
  167.  
  168. ;;; Preparations to be done at load time
  169.  
  170. (defvar auto-save-directory-fallback (expand-file-name "~/autosave/")
  171.   ;; not user-variable-p, see above
  172.   "Directory used for local autosaving of remote files if
  173. both `auto-save-directory' and `ange-ftp-auto-save-remotely' are nil.
  174. Also used if a working directory to be used for autosaving is not writable.
  175. This *must* always be the name of directory that exists or can be
  176. created by you, never nil.")
  177.  
  178. (defvar auto-save-hash-directory
  179.   (expand-file-name "hash/" (or auto-save-directory
  180.                 auto-save-directory-fallback))
  181.   "If non-nil, directory used for hashed autosave filenames.")
  182.  
  183. (defun auto-save-check-directory (var)
  184.   (let ((dir (symbol-value var)))
  185.     (if (null dir)
  186.     nil
  187.       ;; Expand and store back into the variable
  188.       (set var (setq dir (expand-file-name dir)))
  189.       ;; Make sure directory exists
  190.       (if (file-directory-p dir)
  191.       nil
  192.     ;; Else we create and chmod 0700 the directory
  193.     (setq dir (directory-file-name dir)) ; some systems need this
  194.     (if (fboundp 'make-directory)    ; V19 or tree dired
  195.         (make-directory dir)
  196.       (call-process "mkdir" nil nil nil dir))
  197.     (set-file-modes dir (* 7 8 8))))))
  198.  
  199. (mapcar (function auto-save-check-directory)
  200.     '(auto-save-directory auto-save-directory-fallback))
  201.  
  202. (and auto-save-hash-p
  203.      (auto-save-check-directory 'auto-save-hash-directory))
  204.  
  205.  
  206. ;;; Computing an autosave name for a file and vice versa
  207.  
  208. (defun make-auto-save-file-name ();; redefines files.el
  209.   ;; auto-save-file-name-p need not be redefined.
  210.  
  211.   "Return file name to use for auto-saves of current buffer.
  212. Does not consider `auto-save-visited-file-name'; that is checked
  213. before calling this function.
  214.  
  215. Offers to autosave all files in the same `auto-save-directory'.  All
  216. autosave files can then be recovered at once with function
  217. `recover-all-files'.
  218.  
  219. Takes care to make autosave files for files accessed through ange-ftp
  220. be local files if variable `ange-ftp-auto-save-remotely' is nil.
  221.  
  222. Takes care of slashes in buffer names to prevent autosave errors.
  223.  
  224. Takes care that autosave files for buffers not visiting any file (such
  225. as `*mail*') from two simultaneous Emacses don't collide by prepending
  226. the Emacs pid.
  227.  
  228. Uses 14 character autosave names if `auto-save-hash-p' is true.
  229.  
  230. Autosaves even if the current directory is not writable, using
  231. directory `auto-save-directory-fallback'.
  232.  
  233. You can redefine this for customization (he he :-).
  234. See also function `auto-save-file-name-p'."
  235.  
  236.   ;; We have to be very careful about not signalling an error in this
  237.   ;; function since files.el does not provide for this (e.g. find-file
  238.   ;; would fail for each new file).
  239.  
  240.   (condition-case error-data
  241.       (let* ((file-name (or (and (boundp 'buffer-file-truename) ; From jwz,
  242.                  buffer-file-truename) ; for Emacs 19?
  243.                 buffer-file-name))
  244.          ;; So autosavename looks like #%...#, roughly as with the
  245.          ;; old make-auto-save-file-name function.  The
  246.          ;; make-temp-name inserts the pid of this Emacs: this
  247.          ;; avoids autosaving from two Emacses into the same file.
  248.          ;; It cannot be recovered automatically then because in
  249.          ;; the next Emacs session (the one after the crash) the
  250.          ;; pid will be different, but file-less buffers like
  251.          ;; *mail* must be recovered manually anyway.
  252.          (name-prefix (if file-name nil (make-temp-name "#%")))
  253.          (save-name (or file-name
  254.                 ;; Prevent autosave errors.  Buffername
  255.                 ;; (to become non-dir part of filename) will
  256.                 ;; be unslashified twice.  Don't care.
  257.                 (auto-save-unslashify-name (buffer-name))))
  258.          (remote-p (and (stringp file-name)
  259.                 (fboundp 'ange-ftp-ftp-path)
  260.                 (ange-ftp-ftp-path file-name))))
  261.     ;; Return the appropriate auto save file name:
  262.     (expand-file-name;; a buffername needs this, a filename not
  263.      (if remote-p
  264.          (if ange-ftp-auto-save-remotely
  265.          (auto-save-name-in-same-directory save-name)
  266.            ;; We have to use the `fixed-directory' now since the
  267.            ;; `same-directory' would be remote.
  268.            ;; It will use the fallback if needed.
  269.            (auto-save-name-in-fixed-directory save-name))
  270.        ;; Else it is a local file (or a buffer without a file, hence
  271.        ;; the name-prefix).
  272.        ;; Hashed files always go into the special hash dir, never
  273.        ;; in the same directory, to make recognizing reliable.
  274.        (if (or auto-save-directory auto-save-hash-p)
  275.            (auto-save-name-in-fixed-directory save-name name-prefix)
  276.          (auto-save-name-in-same-directory save-name name-prefix)))))
  277.  
  278.     ;; If any error occurs in the above code, return what the old
  279.     ;; version of this function would have done.  It is not ok to
  280.     ;; return nil, e.g., when after-find-file tests
  281.     ;; file-newer-than-file-p, nil would bomb.
  282.  
  283.     (error (progn
  284.          (message "make-auto-save-file-name %s" error-data)
  285.          (sit-for 2)
  286.          (if buffer-file-name
  287.          (concat (file-name-directory buffer-file-name)
  288.              "#"
  289.              (file-name-nondirectory buffer-file-name)
  290.              "#")
  291.            (expand-file-name (concat "#%" (buffer-name) "#")))))))
  292.  
  293. (defun auto-save-original-name (savename)
  294.   "Reverse of `make-auto-save-file-name'.
  295. Returns nil if SAVENAME was not associated with a file (e.g., it came
  296. from an autosaved `*mail*' buffer) or does not appear to be an
  297. autosave file at all.
  298. Hashed files are not understood, see `auto-save-hash-p'."
  299.   (let ((basename (file-name-nondirectory savename))
  300.     (savedir (file-name-directory savename)))
  301.     (cond ((or (not (auto-save-file-name-p basename))
  302.            (string-match "^#%" basename))
  303.        nil)
  304.       ;; now we know it looks like #...# thus substring is safe to use
  305.       ((or (equal savedir auto-save-directory) ; 2nd arg may be nil
  306.            (equal savedir auto-save-directory-fallback))
  307.        ;; it is of the `-fixed-directory' type
  308.        (auto-save-slashify-name (substring basename 1 -1)))
  309.       (t
  310.        ;; else it is of `-same-directory' type
  311.        (concat savedir (substring basename 1 -1))))))
  312.  
  313. (defun auto-save-name-in-fixed-directory (filename &optional prefix)
  314.   ;; Unslashify and enclose the whole FILENAME in `#' to make an auto
  315.   ;; save file in the auto-save-directory, or if that is nil, in
  316.   ;; auto-save-directory-fallback (which must be the name of an
  317.   ;; existing directory).  If the results would be too long for 14
  318.   ;; character filenames, and `auto-save-hash-p' is set, hash FILENAME
  319.   ;; into a shorter name.
  320.   ;; Optional PREFIX is string to use instead of "#" to prefix name.
  321.   (let ((base-name (concat (or prefix "#")
  322.                (auto-save-unslashify-name filename)
  323.                "#")))
  324.     (if (and auto-save-hash-p
  325.          auto-save-hash-directory
  326.          (> (length base-name) 14))
  327.     (expand-file-name (auto-save-cyclic-hash-14 filename)
  328.               auto-save-hash-directory)
  329.       (expand-file-name base-name
  330.             (or auto-save-directory
  331.                 auto-save-directory-fallback)))))
  332.  
  333. (defun auto-save-name-in-same-directory (filename &optional prefix)
  334.   ;; Enclose the non-directory part of FILENAME in `#' to make an auto
  335.   ;; save file in the same directory as FILENAME.  But if this
  336.   ;; directory is not writable, use auto-save-directory-fallback.
  337.   ;; FILENAME is assumed to be in non-directory form (no trailing slash).
  338.   ;; It may be a name without a directory part (pesumably it really
  339.   ;; comes from a buffer name then), the fallback is used then.
  340.   ;; Optional PREFIX is string to use instead of "#" to prefix name.
  341.   (let ((directory (file-name-directory filename)))
  342.     (or (null directory)
  343.     (file-writable-p directory)
  344.     (setq directory auto-save-directory-fallback))
  345.     (concat directory            ; (concat nil) is ""
  346.         (or prefix "#")
  347.         (file-name-nondirectory filename)
  348.         "#")))
  349.  
  350. (defun auto-save-unslashify-name (s)
  351.   ;;  "Quote any slashes in string S by replacing them with the two
  352.   ;;characters `\\!'.
  353.   ;;Also, replace any backslash by double backslash, to make it one-to-one."
  354.   (let ((limit 0))
  355.     (while (string-match "[/\\]" s limit)
  356.       (setq s (concat (substring s 0 (match-beginning 0))
  357.               (if (string= (substring s
  358.                           (match-beginning 0)
  359.                           (match-end 0))
  360.                    "/")
  361.               "\\!"
  362.             "\\\\")
  363.               (substring s (match-end 0))))
  364.       (setq limit (1+ (match-end 0)))))
  365.   s)
  366.  
  367. (defun auto-save-slashify-name (s)
  368.   ;;"Reverse of `auto-save-unslashify-name'."
  369.   (let (pos)
  370.     (while (setq pos (string-match "\\\\[\\!]" s pos))
  371.       (setq s (concat (substring s 0 pos)
  372.               (if (eq ?! (aref s (1+ pos))) "/" "\\")
  373.               (substring s (+ pos 2)))
  374.         pos (1+ pos))))
  375.   s)
  376.  
  377.  
  378. ;;; Hashing for autosave names
  379.  
  380. ;;; Hashing function contributed by Andy Norman <ange@hplb.hpl.hp.com>
  381. ;;; based upon C code from pot@fly.cnuce.cnr.IT (Francesco Potorti`).
  382.  
  383. (defun auto-save-cyclic-hash-14 (s)
  384.   ;;   "Hash string S into a string of length 14.
  385.   ;; The resulting string consists of hexadecimal digits [0-9a-f].
  386.   ;; In particular, it contains no slash, so it can be used as autosave name."
  387.   (let ((crc (make-string 8 0))
  388.     result)
  389.     (mapcar
  390.      (function
  391.       (lambda (new)
  392.     (setq new (+ new (aref crc 7)))
  393.     (aset crc 7 (aref crc 6))
  394.     (aset crc 6 (+ (aref crc 5) new))
  395.     (aset crc 5 (aref crc 4))
  396.     (aset crc 4 (aref crc 3))
  397.     (aset crc 3 (+ (aref crc 2) new))
  398.     (aset crc 2 (aref crc 1))
  399.     (aset crc 1 (aref crc 0))
  400.     (aset crc 0 new)))
  401.      s)
  402.     (setq result (format "%02x%02x%02x%02x%02x%02x%02x"
  403.              (aref crc 0)
  404.              (aref crc 1)
  405.              (aref crc 2)
  406.              (aref crc 3)
  407.              (aref crc 4)
  408.              (aref crc 5)
  409.              (aref crc 6)
  410.              (aref crc 7)))
  411.     result))
  412.  
  413.  
  414.  
  415. ;;; Recovering files
  416.  
  417. (defun recover-all-files ()
  418.   "Do recover-file for all autosave files which are current.
  419. Only works if you have a non-nil `auto-save-directory'.
  420. Hashed files (see `auto-save-hash-p') are not understood, use
  421. `recover-file' to recover them individually."
  422.   (interactive)
  423.   (let ((savefiles (directory-files auto-save-directory t "^#"))
  424.     afile                ; the auto save file
  425.     file                ; its original file
  426.     (total 0)            ; # of files offered to recover
  427.     (count 0))            ; # of files actually recovered
  428.     (or (equal auto-save-directory auto-save-directory-fallback)
  429.     (setq savefiles
  430.           (append savefiles
  431.               (directory-files auto-save-directory-fallback t "^#"))))
  432.     (while savefiles
  433.       (setq afile (car savefiles)
  434.         file (auto-save-original-name afile)
  435.         savefiles (cdr savefiles))
  436.       (if file
  437.       (cond ((not (file-newer-than-file-p afile file))
  438.          (message "autosave file \"%s\" is not current." afile)
  439.          (sit-for 2))
  440.         (t
  441.          (setq total (1+ total))
  442.          (with-output-to-temp-buffer "*Directory*"
  443.            (call-process "ls" nil standard-output nil
  444.                  "-l" afile file))
  445.          (if (yes-or-no-p (format "Recover %s from auto save file? "
  446.                       file))
  447.              (let* ((obuf (current-buffer))
  448.                 (buf (set-buffer (find-file-noselect file t)))
  449.                 (buffer-read-only nil))
  450.                (erase-buffer)
  451.                (insert-file-contents afile nil)
  452.                (after-find-file nil)
  453.                (setq buffer-auto-save-file-name nil)
  454.                (setq count (1+ count))
  455.                (message "\
  456. Auto-save off in buffer \"%s\" till you do M-x auto-save-mode."
  457.                 (buffer-name))
  458.                (set-buffer obuf)
  459.                (sit-for 1)))))))
  460.     (if (zerop total)
  461.     (message "Nothing to recover.")
  462.       (message "%d/%d file%s recovered." count total (if (= count 1) "" "s"))))
  463.   (if (get-buffer "*Directory*") (kill-buffer "*Directory*")))
  464.  
  465. (provide 'auto-save)
  466.